﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static TestBC25Module.Form_Main;

namespace TestBC25Module
{
    public class BC25
    {
        public SerialPort serialPort;
        public bool EnableFlag = false;
        public string ComNameStr;
        public string BaudRateStr;
        public string ParityStr;
        public BC25Data bC25Data_Add;//向队列中添加的等待发送的事务
        private BC25Data bC25Data_Sub = new BC25Data(true);//从队列中取出的已发送并且等待应答的事务
        private ManualResetEvent _event = new ManualResetEvent(false);
        private System.Timers.Timer timer1;
        private ConcurrentQueue<BC25Data> DataSendQueue = new ConcurrentQueue<BC25Data>();
        private ConcurrentQueue<byte[]> DataRecvQueue = new ConcurrentQueue<byte[]>();
        private byte[] RecvBuf = new byte[4000];//接收缓冲区
        private int RecvBufLength = 0;//接收缓冲区内有多少数据

        /* 构造函数，实例化后会自动创建一个定时器和线程，用于顺序处理串口数据的发送和接收解析 */
        public BC25(SerialPort serialPort, bool enableFlag, string comName, string baudRate, string parity)//构造函数
        {
            this.serialPort = serialPort;
            this.EnableFlag = enableFlag;
            this.ComNameStr = comName;
            this.BaudRateStr = baudRate;
            this.ParityStr = parity;

            Thread thread = new Thread(threadRunning);
            thread.IsBackground = true;//设置线程为后台线程,那样进程里就不会有未关闭的程序了
            thread.Start();
            timer1 = new System.Timers.Timer();
            timer1.Enabled = true;
            timer1.Interval = 300;
            timer1.Start();
            timer1.Elapsed += new System.Timers.ElapsedEventHandler(WakeupThread);
        }

        /* 定时器定时执行，发送数据完成后，设定超时时间并开启定时器，同时休眠串口处理线程。应答接收完成后暂停定时器 */
        private void WakeupThread(object source, System.Timers.ElapsedEventArgs e)//定时器，用于定时唤醒threadRunning（串口通信的事务处理线程）
        {
            _event.Set(); //运行线程，_event.Set();用于代替thread.Resume();继续
        }

        /* 串口通信的事务处理线程。顺序执行和循环执行串口发送、等待接收、超时判断、接收解码。*/
        private void threadRunning()//串口通信的事务处理线程
        {
            string logStr = "";
            while (true)
            {
                _event.WaitOne();//
                logStr = "";
                while (!DataRecvQueue.IsEmpty)//将接收到的数据取出
                {
                    byte[] tmpbyte;
                    DataRecvQueue.TryDequeue(out tmpbyte);
                    //tmpbyte.CopyTo(RecvBuf, 0);
                    //RecvBufLength += tmpbyte.Length;
                    for (int i = 0; i < tmpbyte.Length; i++)
                    {
                        RecvBuf[RecvBufLength] = tmpbyte[i];
                        RecvBufLength++;
                    }
                    Thread.Sleep(300);
                }
                if (RecvBufLength > 0)//判断是否有未处理的数据
                {
                    byte[] tmpRecvBuf = new byte[RecvBufLength];
                    for (int i = 0; i < RecvBufLength; i++)//总结出一个数组，里面是全部的已接收的内容
                    {
                        tmpRecvBuf[i] = RecvBuf[i];
                    }
                    if (!bC25Data_Sub.okFlag)//刚刚发送的数据还没收到应答
                    {
                        string s = System.Text.Encoding.ASCII.GetString(tmpRecvBuf);//将收到的数据转换成字符串
                        int m = s.IndexOf("ERROR", StringComparison.CurrentCultureIgnoreCase);//查找收到的字符串里面是否包含该字符串
                        int n = s.IndexOf("OK", StringComparison.CurrentCultureIgnoreCase);//查找收到的字符串里面是否包含该字符串
                        if ((m >= 0) || (n >= 0))//应答接收完成了
                        {
                            Decode_DataReceived(tmpRecvBuf);
                            RecvBufLength = 0;
                            timer1.Stop();
                            timer1.Interval = 300;
                            timer1.Start();
                        }
                        else
                        {
                            if (DateTime.Now > bC25Data_Sub.overDateTime)//判断等待应答是否超时
                            {
                                bC25Data_Sub.okFlag = true;//设定为完成
                                logStr += "Sorry! Wait Time is " + bC25Data_Sub.waitTime_ms + "ms, but over time.\r\n";
                                logStr += "[Rx(byte)]=" + byteToHexStr(tmpRecvBuf) + "[Rx(char)]=" + System.Text.Encoding.ASCII.GetString(tmpRecvBuf) + "\r\n";
                                RecvBufLength = 0;
                                timer1.Stop();
                                timer1.Interval = 300;
                                timer1.Start();
                            }
                        }
                    }
                    else
                    {
                        logStr += "[Rx(byte)]=" + byteToHexStr(tmpRecvBuf).Replace(",", " ") + "\r\n" + 
                            "[Rx(char)]=" + System.Text.Encoding.ASCII.GetString(tmpRecvBuf).Replace("\r\n", "<CR><LF>");
                        RecvBufLength = 0;
                    }
                }
                else//上次发送，已经有了应答了。则应答发送后面的数据
                {
                    if ((!bC25Data_Sub.okFlag) && (DateTime.Now > bC25Data_Sub.overDateTime))
                    {
                        logStr += "Sorry! Wait Time is " + bC25Data_Sub.waitTime_ms + "ms, but over time.\r\n";
                        bC25Data_Sub.okFlag = true;
                    }
                    if (!DataSendQueue.IsEmpty)//不为空，则有内容需要发送
                    {
                        DataSendQueue.TryDequeue(out bC25Data_Sub);
                        Pro_DataSend(bC25Data_Sub.sendDataBuf);
                        bC25Data_Sub.overDateTime = DateTime.Now.AddMilliseconds(bC25Data_Sub.waitTime_ms);
                        timer1.Stop();
                        timer1.Interval = bC25Data_Sub.waitTime_ms;
                        timer1.Start();
                        logStr += "[Tx(byte)]=" + byteToHexStr(bC25Data_Sub.sendDataBuf).Replace(",", " ") + "\r\n" +
                            "[Tx(char)]=" + System.Text.Encoding.ASCII.GetString(bC25Data_Sub.sendDataBuf).Replace("\r\n", "<CR><LF>");
                    }
                }
                if(logStr.Length > 0)
                {
                    Logger.WriteLog(logStr);
                }
                _event.Reset();//用于代替Thread.Suspend()暂停
            }
        }

        /* 字节数组转16进制字符串 */
        private string byteToHexStr(byte[] bytes)//字节数组转16进制字符串
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2") + ",";
                }
            }
            return returnStr;
        }

        /* 打开串口 */
        public void OpenCom()//打开串口
        {
            if (!EnableFlag) return;
            if (serialPort.IsOpen)//判断串口是否已经被打开了
            {
                Logger.WriteLog(ComNameStr + " Port is opened");//出错，则写日志
            }
            else
            {
                serialPort.PortName = ComNameStr;
                serialPort.BaudRate = Convert.ToInt32(BaudRateStr);
                if (ParityStr.Equals("NONE"))
                    serialPort.Parity = Parity.None;
                else if (ParityStr.Equals("EVEN"))
                    serialPort.Parity = Parity.Even;
                else if (ParityStr.Equals("ODD"))
                    serialPort.Parity = Parity.Odd;
                else
                    serialPort.Parity = Parity.None;
                try
                {
                    serialPort.Open();//尝试打开串口
                    serialPort.DataReceived += new SerialDataReceivedEventHandler(Pro_DataReceived);//串口接收处理函数
                }
                catch (Exception err)
                {
                    Logger.WriteLog("Com Port Open Error, error:" + err);//出错，则写日志
                }
            }
        }

        /* 关闭串口 */
        public void CloseCom()//关闭串口
        {
            if (!EnableFlag)
            {
                Logger.WriteLog("Sorry! Module.Enable = false, please enable it.");
                return;
            }
            if (serialPort.IsOpen)//判断串口是否已经被打开了
            {
                try
                {
                    serialPort.Close();
                }
                catch (Exception err)
                {
                    Console.WriteLine("Com Port Close Error, error:" + err);
                }
            }
        }

        /* 串口接收数据 */
        private void Pro_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口接收数据，放入队列中，并唤醒线程处理它
        {
            //string str = serialPort.ReadExisting();//字符串方式读
            byte[] tmpReceiveBuf = new byte[serialPort.BytesToRead];//接收到多少个字节的数据，则创建一个临时接收缓冲区
            serialPort.Read(tmpReceiveBuf, 0, tmpReceiveBuf.Length);//将收到的数据移动至临时接收缓冲区
            DataRecvQueue.Enqueue(tmpReceiveBuf);//将本次接收到的数据移入队列，等待线程thread处理
            _event.Set(); //运行线程（线程会立即从阻塞态变为运行态），_event.Set();用于代替thread.Resume();继续
        }

        /* 串口发送数据 */
        private void Pro_DataSend(byte[] buf)//串口发送数据
        {
            if (!EnableFlag)
            {
                Logger.WriteLog("Sorry! Module.Enable = false, please enable it.");
                return;
            }
            if (!serialPort.IsOpen)
            {
                //OpenCom();
                Logger.WriteLog("Sorry! Com Port is not opened.");
                return;
            }
            try
            {
                serialPort.Write(buf, 0, buf.Length);
            }
            catch (Exception e)
            {
                Logger.WriteLog("Com Port Data Send Error, error:" + e);//出错，则写日志
            }
        }

        /* 解码收到的数据 */
        private void Decode_DataReceived(byte[] recvData)//解码收到的数据
        {
            string cmd = bC25Data_Sub.commandStr;
            if (cmd.Equals("ATI", StringComparison.OrdinalIgnoreCase)) decode_ATI(recvData);
            else if (cmd.Equals("ATE", StringComparison.OrdinalIgnoreCase)) decode_ATE(recvData);
            else if (cmd.Equals("AT&W", StringComparison.OrdinalIgnoreCase)) decode_ATW(recvData);
            else if (cmd.Equals("AT+CGMI", StringComparison.OrdinalIgnoreCase)) decode_CGMI(recvData);
            else if (cmd.Equals("AT+CGMM", StringComparison.OrdinalIgnoreCase)) decode_CGMM(recvData);
            else if (cmd.Equals("AT+CGMR", StringComparison.OrdinalIgnoreCase)) decode_CGMR(recvData);
            else if (cmd.Equals("AT+CGSN", StringComparison.OrdinalIgnoreCase)) decode_CGSN(recvData);
            else if (cmd.Equals("AT+CMEE", StringComparison.OrdinalIgnoreCase)) decode_CMEE(recvData);
            else if (cmd.Equals("AT+IPR", StringComparison.OrdinalIgnoreCase)) decode_IPR(recvData);
            else if (cmd.Equals("AT+QEAUART", StringComparison.OrdinalIgnoreCase)) decode_QEAUART(recvData);
            else if (cmd.Equals("AT+CPIN", StringComparison.OrdinalIgnoreCase)) decode_CPIN(recvData);
            else if (cmd.Equals("AT+CIMI", StringComparison.OrdinalIgnoreCase)) decode_CIMI(recvData);
            else if (cmd.Equals("AT+QCCID", StringComparison.OrdinalIgnoreCase)) decode_QCCID(recvData);
            else if (cmd.Equals("AT+CSCON", StringComparison.OrdinalIgnoreCase)) decode_CSCON(recvData);
            else Logger.WriteLog("[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" + 
                "[Rx(char)]=" + System.Text.Encoding.ASCII.GetString(recvData).Replace("\r\n", "<CR><LF>"));
            bC25Data_Sub.okFlag = true;
        }

        /// <summary>
        /// 命令编码，组织要发送的数据，填入多线程处理队列
        /// </summary>
        public void encode(string cmdstr, string param, int time)//命令编码
        {
            if (!EnableFlag)
            {
                Logger.WriteLog("Sorry! Module.Enable = false, please enable it.");
                return;
            }
            byte[] sendDataBuf = System.Text.Encoding.ASCII.GetBytes(param);
            BC25Data bC25Data = new BC25Data(cmdstr, sendDataBuf, time);
            DataSendQueue.Enqueue(bC25Data);
        }

        //--------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// ATI应答解码
        /// </summary>
        private void decode_ATI(byte[] recvData)//ATI应答解码
        {
            string s = System.Text.Encoding.ASCII.GetString(recvData);
            string logStr = "[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" + 
                            "[Rx(char)]=" + s.Replace("\r\n", "<CR><LF>");
            int Error_Num = s.IndexOf("ERROR", StringComparison.CurrentCultureIgnoreCase);
            int OK_Num = s.IndexOf("OK", StringComparison.CurrentCultureIgnoreCase);
            if (Error_Num >= 0)//判断字符串中是否包含Error
            {
                if (Callback_Modify_textBox_ObjectId != null)
                {
                    Callback_Modify_textBox_ObjectId("");
                }
                if (Callback_Modify_textBox_Revision != null)
                {
                    Callback_Modify_textBox_Revision("");
                }
            }
            else if (OK_Num >= 0)//判断字符串中是否包含OK
            {
                int Quectel_Ltd_Num = s.IndexOf("Quectel_Ltd\r\n", StringComparison.CurrentCultureIgnoreCase);
                
                int Revision_Num = s.IndexOf("Revision: ", StringComparison.CurrentCultureIgnoreCase);
                if ((Quectel_Ltd_Num >= 0) && (Revision_Num >= 0))
                {
                    int startNum = Quectel_Ltd_Num + "Quectel_Ltd\r\n".Length;
                    int strlength = s.Substring(startNum).IndexOf("\r\n");
                    if (startNum < Revision_Num)
                    {
                        if (Callback_Modify_textBox_ObjectId != null)
                        {
                            Callback_Modify_textBox_ObjectId(s.Substring(startNum, strlength));
                        }

                    }
                    startNum = Revision_Num + "Revision: ".Length;
                    strlength = s.Substring(startNum).IndexOf("\r\n");
                    if (startNum < OK_Num)
                    {
                        if (Callback_Modify_textBox_Revision != null)
                        {
                            Callback_Modify_textBox_Revision(s.Substring(startNum, strlength));
                        }
                    }
                }
                else
                {
                    if (Callback_Modify_textBox_ObjectId != null)
                    {
                        Callback_Modify_textBox_ObjectId("");
                    }
                    if (Callback_Modify_textBox_Revision != null)
                    {
                        Callback_Modify_textBox_Revision("");
                    }
                }
            }
            else
            {
                if (Callback_Modify_textBox_ObjectId != null)
                {
                    Callback_Modify_textBox_ObjectId("");
                }
                if (Callback_Modify_textBox_Revision != null)
                {
                    Callback_Modify_textBox_Revision("");
                }
                logStr += "返回的數據中既不包含OK，又不包含ERROR，無法解碼！";
            }
            Logger.WriteLog(logStr);
        }

        public delegate_EventHandler_textBox_ObjectId Callback_Modify_textBox_ObjectId = null;//设备类型标识
        public delegate_EventHandler_textBox_Revision Callback_Modify_textBox_Revision = null;//当前软件版本号

        //--------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// ATE应答解码
        /// </summary>
        private void decode_ATE(byte[] recvData)//ATE应答解码
        {
            string logStr = "[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" + 
                            "[Rx(char)]=" + System.Text.Encoding.ASCII.GetString(recvData).Replace("\r\n", "<CR><LF>") + "\r\n";
            Logger.WriteLog(logStr);
        }

        //--------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// AT&W应答解码
        /// </summary>
        private void decode_ATW(byte[] recvData)//AT&W应答解码
        {
            string logStr = "[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" + 
                            "[Rx(char)]=" + System.Text.Encoding.ASCII.GetString(recvData).Replace("\r\n", "<CR><LF>") + "\r\n";
            Logger.WriteLog(logStr);
        }

        //--------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// AT+CGMI应答解码
        /// </summary>
        private void decode_CGMI(byte[] recvData)//AT+CGMI应答解码
        {
            string s = System.Text.Encoding.ASCII.GetString(recvData);
            string logStr = "[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" + 
                            "[Rx(char)]=" + s.Replace("\r\n", "<CR><LF>");
            int Error_Num = s.IndexOf("ERROR", StringComparison.CurrentCultureIgnoreCase);
            int OK_Num = s.IndexOf("OK", StringComparison.CurrentCultureIgnoreCase);
            if (Error_Num >= 0)//判断字符串中是否包含Error
            {
                if (Callback_Modify_textBox_ObjectId2 != null)
                {
                    Callback_Modify_textBox_ObjectId2("");
                }
                if (Callback_Modify_textBox_Revision2 != null)
                {
                    Callback_Modify_textBox_Revision2("");
                }
            }
            else if (OK_Num >= 0)//判断字符串中是否包含OK
            {
                int Quectel_Ltd_Num = s.IndexOf("Quectel_Ltd\r\n", StringComparison.CurrentCultureIgnoreCase);
                int Revision_Num = s.IndexOf("Revision: ", StringComparison.CurrentCultureIgnoreCase);
                if ((Quectel_Ltd_Num >= 0) && (Revision_Num >= 0))
                {
                    int startNum = Quectel_Ltd_Num + "Quectel_Ltd\r\n".Length;
                    int strlength = s.Substring(startNum).IndexOf("\r\n");
                    if (startNum < Revision_Num)
                    {
                        if (Callback_Modify_textBox_ObjectId2 != null)
                        {
                            Callback_Modify_textBox_ObjectId2(s.Substring(startNum, strlength));
                        }

                    }
                    startNum = Revision_Num + "Revision: ".Length;
                    strlength = s.Substring(startNum).IndexOf("\r\n");
                    if (startNum < OK_Num)
                    {
                        if (Callback_Modify_textBox_Revision2 != null)
                        {
                            Callback_Modify_textBox_Revision2(s.Substring(startNum, strlength));
                        }
                    }
                }
                else
                {
                    if (Callback_Modify_textBox_ObjectId2 != null)
                    {
                        Callback_Modify_textBox_ObjectId2("");
                    }
                    if (Callback_Modify_textBox_Revision2 != null)
                    {
                        Callback_Modify_textBox_Revision2("");
                    }
                }
            }
            else
            {
                if (Callback_Modify_textBox_ObjectId2 != null)
                {
                    Callback_Modify_textBox_ObjectId2("");
                }
                if (Callback_Modify_textBox_Revision2 != null)
                {
                    Callback_Modify_textBox_Revision2("");
                }
                logStr += "返回的數據中既不包含OK，又不包含ERROR，無法解碼！";
            }
            Logger.WriteLog(logStr);
        }

        public delegate_EventHandler_textBox_ObjectId2 Callback_Modify_textBox_ObjectId2 = null;//设备类型标识
        public delegate_EventHandler_textBox_Revision2 Callback_Modify_textBox_Revision2 = null;//设备版本号

        //--------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// AT+CGMM应答解码
        /// </summary>
        private void decode_CGMM(byte[] recvData)//AT+CGMM应答解码
        {
            string s = System.Text.Encoding.ASCII.GetString(recvData);
            string logStr = "[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" +
                            "[Rx(char)]=" + s.Replace("\r\n", "<CR><LF>");
            int Error_Num = s.IndexOf("ERROR", StringComparison.CurrentCultureIgnoreCase);
            int OK_Num = s.IndexOf("OK", StringComparison.CurrentCultureIgnoreCase);
            if (Error_Num >= 0)//判断字符串中是否包含Error
            {
                if (Callback_Modify_textBox_ObjectId3 != null)
                {
                    Callback_Modify_textBox_ObjectId3("");
                }
            }
            else if (OK_Num >= 0)//判断字符串中是否包含OK
            {
                int head_Num = s.IndexOf("\r\n", StringComparison.CurrentCultureIgnoreCase);
                int startNum = head_Num + "\r\n".Length;
                int strlength = s.Substring(startNum).IndexOf("\r\n\r\n");
                if ((OK_Num > 0) && (startNum < OK_Num))
                {
                    if (Callback_Modify_textBox_ObjectId3 != null)
                    {
                        Callback_Modify_textBox_ObjectId3(s.Substring(startNum, strlength));
                    }
                }
                else
                {
                    if (Callback_Modify_textBox_ObjectId3 != null)
                    {
                        Callback_Modify_textBox_ObjectId3("");
                    }
                }
            }
            else
            {
                if (Callback_Modify_textBox_ObjectId3 != null)
                {
                    Callback_Modify_textBox_ObjectId3("");
                }
                logStr += "返回的數據中既不包含OK，又不包含ERROR，無法解碼！";
            }
            Logger.WriteLog(logStr);
        }

        public delegate_EventHandler_textBox_ObjectId3 Callback_Modify_textBox_ObjectId3 = null;//设备类型标识

        //--------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// AT+CGMR应答解码
        /// </summary>
        private void decode_CGMR(byte[] recvData)//AT+CGMR应答解码
        {
            string s = System.Text.Encoding.ASCII.GetString(recvData);
            string logStr = "[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" +
                            "[Rx(char)]=" + s.Replace("\r\n", "<CR><LF>");
            int Error_Num = s.IndexOf("ERROR", StringComparison.CurrentCultureIgnoreCase);
            int OK_Num = s.IndexOf("OK", StringComparison.CurrentCultureIgnoreCase);
            if (Error_Num >= 0)//判断字符串中是否包含Error
            {
                if (Callback_Modify_textBox_Revision3 != null)
                {
                    Callback_Modify_textBox_Revision3("");
                }
            }
            else if (OK_Num >= 0)//判断字符串中是否包含OK
            {
                int Revision_Num = s.IndexOf("Revision: ", StringComparison.CurrentCultureIgnoreCase);
                if (OK_Num > 0 && Revision_Num > 0)
                {
                    int startNum = Revision_Num + "Revision: ".Length;
                    int strlength = s.Substring(startNum).IndexOf("\r\n");
                    if (startNum < OK_Num)
                    {
                        if (Callback_Modify_textBox_Revision3 != null)
                        {
                            Callback_Modify_textBox_Revision3(s.Substring(startNum, strlength));
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_Revision3 != null)
                        {
                            Callback_Modify_textBox_Revision3("");
                        }
                    }
                }
                else
                {
                    if (Callback_Modify_textBox_Revision3 != null)
                    {
                        Callback_Modify_textBox_Revision3("");
                    }
                }
            }
            else
            {
                if (Callback_Modify_textBox_Revision3 != null)
                {
                    Callback_Modify_textBox_Revision3("");
                }
                logStr += "返回的數據中既不包含OK，又不包含ERROR，無法解碼！";
            }
            Logger.WriteLog(logStr);
        }

        public delegate_EventHandler_textBox_Revision3 Callback_Modify_textBox_Revision3 = null;//设备版本号

        //--------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// AT+CGSN应答解码
        /// </summary>
        private void decode_CGSN(byte[] recvData)//AT+CGSN应答解码
        {
            string s = System.Text.Encoding.ASCII.GetString(recvData);
            string logStr = "[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" +
                            "[Rx(char)]=" + s.Replace("\r\n", "<CR><LF>");
            int Error_Num = s.IndexOf("ERROR", StringComparison.CurrentCultureIgnoreCase);
            int OK_Num = s.IndexOf("OK", StringComparison.CurrentCultureIgnoreCase);
            string sendStr = System.Text.Encoding.ASCII.GetString(bC25Data_Sub.sendDataBuf);
            if (Error_Num >= 0)//判断字符串中是否包含Error
            {
                if (sendStr.Equals("AT+CGSN", StringComparison.OrdinalIgnoreCase))
                {
                    if (Callback_Modify_textBox_CGSN3 != null)
                    {
                        Callback_Modify_textBox_CGSN3("");
                    }
                }
                else if (sendStr.Equals("AT+CGSN=?", StringComparison.OrdinalIgnoreCase))
                {
                    if (Callback_Modify_textBox_CGSN1 != null)
                    {
                        Callback_Modify_textBox_CGSN1("");
                    }
                }
                else
                {
                    if (Callback_Modify_textBox_CGSN3 != null)
                    {
                        Callback_Modify_textBox_CGSN3("");
                    }
                }
            }
            else if (OK_Num >= 0)//判断字符串中是否包含OK
            {
                if (sendStr.Equals("AT+CGSN\r", StringComparison.OrdinalIgnoreCase))
                {
                    if (OK_Num > 0)
                    {
                        if (Callback_Modify_textBox_CGSN3 != null)
                        {
                            Callback_Modify_textBox_CGSN3("<sn>=" + s.Substring(0, OK_Num));
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_CGSN3 != null)
                        {
                            Callback_Modify_textBox_CGSN3("");
                        }
                    }
                }
                else if (sendStr.Equals("AT+CGSN=?\r", StringComparison.OrdinalIgnoreCase))
                {
                    int CGSN_Num = s.IndexOf("+CGSN:", StringComparison.CurrentCultureIgnoreCase);
                    if (OK_Num > 0 && CGSN_Num > 0)
                    {
                        int startNum = CGSN_Num + "+CGSN:".Length;
                        if (startNum < OK_Num)
                        {
                            if (Callback_Modify_textBox_CGSN1 != null)
                            {
                                Callback_Modify_textBox_CGSN1("<snt>列表=" + s.Substring(startNum, OK_Num - startNum));
                            }
                        }
                        else
                        {
                            if (Callback_Modify_textBox_CGSN1 != null)
                            {
                                Callback_Modify_textBox_CGSN1("");
                            }
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_CGSN1 != null)
                        {
                            Callback_Modify_textBox_CGSN1("");
                        }
                    }
                }
                else if (sendStr.Equals("AT+CGSN=0\r", StringComparison.OrdinalIgnoreCase))
                {
                    if (OK_Num > 0)
                    {
                        if (Callback_Modify_textBox_CGSN3 != null)
                        {
                            Callback_Modify_textBox_CGSN3("<sn>=" + s.Substring(0, OK_Num));
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_CGSN3 != null)
                        {
                            Callback_Modify_textBox_CGSN3("");
                        }
                    }
                }
                else if (sendStr.Equals("AT+CGSN=1\r", StringComparison.OrdinalIgnoreCase))
                {
                    int CGSN_Num = s.IndexOf("+CGSN:", StringComparison.CurrentCultureIgnoreCase);
                    if (OK_Num > 0 && CGSN_Num > 0)
                    {
                        int startNum = CGSN_Num + "+CGSN:".Length;
                        if (startNum < OK_Num)
                        {
                            if (Callback_Modify_textBox_CGSN3 != null)
                            {
                                Callback_Modify_textBox_CGSN3("<imei>=" + s.Substring(startNum, OK_Num - startNum));
                            }
                        }
                        else
                        {
                            if (Callback_Modify_textBox_CGSN3 != null)
                            {
                                Callback_Modify_textBox_CGSN3("");
                            }
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_CGSN3 != null)
                        {
                            Callback_Modify_textBox_CGSN3("");
                        }
                    }
                }
                else
                {
                    logStr += " 發送的質量不屬於下面的4種（AT+CGSN、AT+CGSN=?、AT+CGSN=0、AT+CGSN=1），因此無法解析接收到的數據！";
                }
                
            }
            else
            {
                logStr += " 返回的數據中既不包含OK，又不包含ERROR，無法解碼！";
            }
            Logger.WriteLog(logStr);
        }

        public delegate_EventHandler_textBox_CGSN1 Callback_Modify_textBox_CGSN1 = null;
        public delegate_EventHandler_textBox_CGSN3 Callback_Modify_textBox_CGSN3 = null;

        //--------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// AT+CMEE应答解码
        /// </summary>
        private void decode_CMEE(byte[] recvData)//AT+CMEE应答解码
        {
            string s = System.Text.Encoding.ASCII.GetString(recvData);
            string logStr = "[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" +
                            "[Rx(char)]=" + s.Replace("\r\n", "<CR><LF>");
            int Error_Num = s.IndexOf("ERROR", StringComparison.CurrentCultureIgnoreCase);
            int OK_Num = s.IndexOf("OK", StringComparison.CurrentCultureIgnoreCase);
            string sendStr = System.Text.Encoding.ASCII.GetString(bC25Data_Sub.sendDataBuf);
            if (Error_Num >= 0)//判断字符串中是否包含Error
            {
                if (sendStr.Equals("AT+CMEE?\r", StringComparison.OrdinalIgnoreCase))
                {
                    if (Callback_Modify_textBox_CMEE1 != null)
                    {
                        Callback_Modify_textBox_CMEE1("");
                    }
                }
                else if (sendStr.Equals("AT+CMEE=?\r", StringComparison.OrdinalIgnoreCase))
                {
                    if (Callback_Modify_textBox_CMEE2 != null)
                    {
                        Callback_Modify_textBox_CMEE2("");
                    }
                }
            }
            else if (OK_Num >= 0)//判断字符串中是否包含OK
            {
                if (sendStr.Equals("AT+CMEE?\r", StringComparison.OrdinalIgnoreCase))
                {
                    int CMEE_Num = s.IndexOf("+CMEE: ", StringComparison.CurrentCultureIgnoreCase);
                    if (OK_Num > 0 && CMEE_Num > 0)
                    {
                        int startNum = CMEE_Num + "+CMEE: ".Length;
                        int strlength = s.Substring(startNum).IndexOf("\r\n");
                        if (startNum < OK_Num)
                        {
                            if (Callback_Modify_textBox_CMEE1 != null)
                            {
                                string tmp = s.Substring(startNum, strlength);
                                
                                if (tmp.Equals("0", StringComparison.OrdinalIgnoreCase))
                                {
                                    Callback_Modify_textBox_CMEE1(tmp + ":禁用結果碼");
                                }
                                else if (tmp.Equals("1", StringComparison.OrdinalIgnoreCase))
                                {
                                    Callback_Modify_textBox_CMEE1(tmp + ":啟用結果碼，結果碼為數值格式");
                                }
                                else if (tmp.Equals("2", StringComparison.OrdinalIgnoreCase))
                                {
                                    Callback_Modify_textBox_CMEE1(tmp + ":啟用結果碼，結果為詳細錯誤信息");
                                }
                                else
                                {
                                    Callback_Modify_textBox_CMEE1(tmp);
                                }
                            }
                        }
                        else
                        {
                            if (Callback_Modify_textBox_CMEE1 != null)
                            {
                                Callback_Modify_textBox_CMEE1("");
                            }
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_CMEE1 != null)
                        {
                            Callback_Modify_textBox_CMEE1("");
                        }
                    }
                }
                else if (sendStr.Equals("AT+CMEE=?\r", StringComparison.OrdinalIgnoreCase))
                {
                    int CMEE_Num = s.IndexOf("+CMEE: ", StringComparison.CurrentCultureIgnoreCase);
                    if (OK_Num > 0 && CMEE_Num > 0)
                    {
                        int startNum = CMEE_Num + "+CMEE: ".Length;
                        int strlength = s.Substring(startNum).IndexOf("\r\n");
                        if (startNum < OK_Num)
                        {
                            if (Callback_Modify_textBox_CMEE2 != null)
                            {
                                Callback_Modify_textBox_CMEE2(s.Substring(startNum, strlength));
                            }
                        }
                        else
                        {
                            if (Callback_Modify_textBox_CMEE2 != null)
                            {
                                Callback_Modify_textBox_CMEE2("");
                            }
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_CMEE2 != null)
                        {
                            Callback_Modify_textBox_CMEE2("");
                        }
                    }
                }
            }
            else
            {
                logStr += "返回的數據中既不包含OK，又不包含ERROR，無法解碼！";
            }
            Logger.WriteLog(logStr);
        }

        public delegate_EventHandler_textBox_CMEE1 Callback_Modify_textBox_CMEE1 = null;
        public delegate_EventHandler_textBox_CMEE2 Callback_Modify_textBox_CMEE2 = null;

        //--------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// AT+IPR应答解码
        /// </summary>
        private void decode_IPR(byte[] recvData)//AT+IPR应答解码
        {
            string s = System.Text.Encoding.ASCII.GetString(recvData);
            string logStr = "[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" +
                            "[Rx(char)]=" + s.Replace("\r\n", "<CR><LF>");
            int Error_Num = s.IndexOf("ERROR", StringComparison.CurrentCultureIgnoreCase);
            int OK_Num = s.IndexOf("OK", StringComparison.CurrentCultureIgnoreCase);
            string sendStr = System.Text.Encoding.ASCII.GetString(bC25Data_Sub.sendDataBuf);
            if (Error_Num >= 0)//判断字符串中是否包含Error
            {
                if (sendStr.Equals("AT+IPR=?\r", StringComparison.OrdinalIgnoreCase))
                {
                    if (Callback_Modify_textBox_IPR1 != null)
                    {
                        Callback_Modify_textBox_IPR1("");
                    }
                }
                else if (sendStr.Equals("AT+IPR?\r", StringComparison.OrdinalIgnoreCase))
                {
                    if (Callback_Modify_textBox_IPR2 != null)
                    {
                        Callback_Modify_textBox_IPR2("");
                    }
                }
            }
            else if (OK_Num >= 0)//判断字符串中是否包含OK
            {
                if (sendStr.Equals("AT+IPR=?\r", StringComparison.OrdinalIgnoreCase))
                {
                    int CMEE_Num = s.IndexOf("+IPR: ", StringComparison.CurrentCultureIgnoreCase);
                    if (OK_Num > 0 && CMEE_Num > 0)
                    {
                        int startNum = CMEE_Num + "+IPR: ".Length;
                        int strlength = s.Substring(startNum).IndexOf("\r\n");
                        if (startNum < OK_Num)
                        {
                            if (Callback_Modify_textBox_IPR1 != null)
                            {
                                Callback_Modify_textBox_IPR1(s.Substring(startNum, strlength));
                            }
                        }
                        else
                        {
                            if (Callback_Modify_textBox_IPR1 != null)
                            {
                                Callback_Modify_textBox_IPR1("");
                            }
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_IPR1 != null)
                        {
                            Callback_Modify_textBox_IPR1("");
                        }
                    }
                }
                else if (sendStr.Equals("AT+IPR?\r", StringComparison.OrdinalIgnoreCase))
                {
                    int CMEE_Num = s.IndexOf("+IPR: ", StringComparison.CurrentCultureIgnoreCase);
                    if (OK_Num > 0 && CMEE_Num > 0)
                    {
                        int startNum = CMEE_Num + "+IPR: ".Length;
                        int strlength = s.Substring(startNum).IndexOf("\r\n");
                        if (startNum < OK_Num)
                        {
                            if (Callback_Modify_textBox_IPR2 != null)
                            {
                                Callback_Modify_textBox_IPR2(s.Substring(startNum, strlength));
                            }
                        }
                        else
                        {
                            if (Callback_Modify_textBox_IPR2 != null)
                            {
                                Callback_Modify_textBox_IPR2("");
                            }
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_IPR2 != null)
                        {
                            Callback_Modify_textBox_IPR2("");
                        }
                    }
                }
            }
            else
            {
                logStr += "返回的數據中既不包含OK，又不包含ERROR，無法解碼！";
            }
            Logger.WriteLog(logStr);
        }

        public delegate_EventHandler_textBox_IPR1 Callback_Modify_textBox_IPR1 = null;
        public delegate_EventHandler_textBox_IPR2 Callback_Modify_textBox_IPR2 = null;

        //--------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// AT+QEAUART应答解码
        /// </summary>
        private void decode_QEAUART(byte[] recvData)//AT+QEAUART应答解码
        {
            string s = System.Text.Encoding.ASCII.GetString(recvData);
            string logStr = "[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" +
                            "[Rx(char)]=" + s.Replace("\r\n", "<CR><LF>");
            int Error_Num = s.IndexOf("ERROR", StringComparison.CurrentCultureIgnoreCase);
            int OK_Num = s.IndexOf("OK", StringComparison.CurrentCultureIgnoreCase);
            string sendStr = System.Text.Encoding.ASCII.GetString(bC25Data_Sub.sendDataBuf);
            if (Error_Num >= 0)//判断字符串中是否包含Error
            {
                if (sendStr.Equals("AT+QEAUART=?\r", StringComparison.OrdinalIgnoreCase))
                {
                    if (Callback_Modify_textBox_QEAUART1 != null)
                    {
                        Callback_Modify_textBox_QEAUART1("");
                    }
                }
                else if (sendStr.Equals("AT+QEAUART?\r", StringComparison.OrdinalIgnoreCase))
                {
                    if (Callback_Modify_textBox_QEAUART2 != null)
                    {
                        Callback_Modify_textBox_QEAUART2("");
                    }
                }
            }
            else if (OK_Num >= 0)//判断字符串中是否包含OK
            {
                if (sendStr.Equals("AT+QEAUART=?\r", StringComparison.OrdinalIgnoreCase))
                {
                    int CMEE_Num = s.IndexOf("+QEAUART:", StringComparison.CurrentCultureIgnoreCase);
                    if (OK_Num > 0 && CMEE_Num > 0)
                    {
                        int startNum = CMEE_Num + "+QEAUART:".Length;
                        int strlength = s.Substring(startNum).IndexOf("\r\n");
                        if (startNum < OK_Num)
                        {
                            if (Callback_Modify_textBox_QEAUART1 != null)
                            {
                                Callback_Modify_textBox_QEAUART1(s.Substring(startNum, strlength));
                            }
                        }
                        else
                        {
                            if (Callback_Modify_textBox_QEAUART1 != null)
                            {
                                Callback_Modify_textBox_QEAUART1("");
                            }
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_QEAUART1 != null)
                        {
                            Callback_Modify_textBox_QEAUART1("");
                        }
                    }
                }
                else if (sendStr.Equals("AT+QEAUART?\r", StringComparison.OrdinalIgnoreCase))
                {
                    int CMEE_Num = s.IndexOf("+QEAUART:", StringComparison.CurrentCultureIgnoreCase);
                    if (OK_Num > 0 && CMEE_Num > 0)
                    {
                        int startNum = CMEE_Num + "+QEAUART:".Length;
                        int strlength = s.Substring(startNum).IndexOf("\r\n");
                        if (startNum < OK_Num)
                        {
                            if (Callback_Modify_textBox_QEAUART2 != null)
                            {
                                Callback_Modify_textBox_QEAUART2(s.Substring(startNum, strlength));
                            }
                        }
                        else
                        {
                            if (Callback_Modify_textBox_QEAUART2 != null)
                            {
                                Callback_Modify_textBox_QEAUART2("");
                            }
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_QEAUART2 != null)
                        {
                            Callback_Modify_textBox_QEAUART2("");
                        }
                    }
                }
            }
            else
            {
                logStr += "返回的數據中既不包含OK，又不包含ERROR，無法解碼！";
            }
            Logger.WriteLog(logStr);
        }

        public delegate_EventHandler_textBox_QEAUART1 Callback_Modify_textBox_QEAUART1 = null;
        public delegate_EventHandler_textBox_QEAUART2 Callback_Modify_textBox_QEAUART2 = null;


        //--------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// AT+CPIN应答解码
        /// </summary>
        private void decode_CPIN(byte[] recvData)//AT+CPIN应答解码
        {
            string s = System.Text.Encoding.ASCII.GetString(recvData);
            string logStr = "[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" +
                            "[Rx(char)]=" + s.Replace("\r\n", "<CR><LF>");
            int Error_Num = s.IndexOf("ERROR", StringComparison.CurrentCultureIgnoreCase);
            int OK_Num = s.IndexOf("OK", StringComparison.CurrentCultureIgnoreCase);
            string sendStr = System.Text.Encoding.ASCII.GetString(bC25Data_Sub.sendDataBuf);
            if (Error_Num >= 0)//判断字符串中是否包含Error
            {
                if (sendStr.Equals("AT+CPIN?\r", StringComparison.OrdinalIgnoreCase))
                {
                    if (Callback_Modify_textBox_CPIN_code != null)
                    {
                        Callback_Modify_textBox_CPIN_code("");
                    }
                }
            }
            else if (OK_Num >= 0)//判断字符串中是否包含OK
            {
                if (sendStr.Equals("AT+CPIN?\r", StringComparison.OrdinalIgnoreCase))
                {
                    int CMEE_Num = s.IndexOf("+CPIN: ", StringComparison.CurrentCultureIgnoreCase);
                    if (OK_Num > 0 && CMEE_Num > 0)
                    {
                        int startNum = CMEE_Num + "+CPIN: ".Length;
                        int strlength = s.Substring(startNum).IndexOf("\r\n");
                        if (startNum < OK_Num)
                        {
                            string codeStr = s.Substring(startNum, strlength);
                            if (codeStr.Equals("READY", StringComparison.OrdinalIgnoreCase)) codeStr += "  無需進一步輸入";
                            else if (codeStr.Equals("SIM PIN", StringComparison.OrdinalIgnoreCase)) codeStr += "  MT 正在等待 USIM PIN 碼";
                            else if (codeStr.Equals("SIM PUK", StringComparison.OrdinalIgnoreCase)) codeStr += "  MT 正在等待 USIM PUK 碼";
                            else if (codeStr.Equals("PH_SP PIN", StringComparison.OrdinalIgnoreCase)) codeStr += "  需要服務提供商個性化密碼";
                            else if (codeStr.Equals("SIM PIN2", StringComparison.OrdinalIgnoreCase)) codeStr += "  MT 正在等待提供 USIM PIN 2 碼。僅在前條命令返回 +CME ERROR: 17 時有效";
                            else if (codeStr.Equals("SIM PUK2", StringComparison.OrdinalIgnoreCase)) codeStr += "  MT 正在等待提供 USIM PUK 2 碼。僅在前條命令返回 +CME ERROR: 18 時有效";
                            else if (codeStr.Equals("NO SIM", StringComparison.OrdinalIgnoreCase)) codeStr += "  SIM 卡未插入";

                            if (Callback_Modify_textBox_CPIN_code != null)
                            {
                                Callback_Modify_textBox_CPIN_code(codeStr);
                            }
                        }
                        else
                        {
                            if (Callback_Modify_textBox_CPIN_code != null)
                            {
                                Callback_Modify_textBox_CPIN_code("");
                            }
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_CPIN_code != null)
                        {
                            Callback_Modify_textBox_CPIN_code("");
                        }
                    }
                }
            }
            else
            {
                logStr += "返回的數據中既不包含OK，又不包含ERROR，無法解碼！";
            }
            Logger.WriteLog(logStr);
        }

        public delegate_EventHandler_textBox_CPIN_code Callback_Modify_textBox_CPIN_code = null;


        //--------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// AT+CIMI应答解码
        /// </summary>
        private void decode_CIMI(byte[] recvData)//AT+CIMI应答解码
        {
            string s = System.Text.Encoding.ASCII.GetString(recvData);
            string logStr = "[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" +
                            "[Rx(char)]=" + s.Replace("\r\n", "<CR><LF>");
            int Error_Num = s.IndexOf("ERROR", StringComparison.CurrentCultureIgnoreCase);
            int OK_Num = s.IndexOf("OK", StringComparison.CurrentCultureIgnoreCase);
            string sendStr = System.Text.Encoding.ASCII.GetString(bC25Data_Sub.sendDataBuf);
            if (Error_Num >= 0)//判断字符串中是否包含Error
            {
                if (sendStr.Equals("AT+CIMI\r", StringComparison.OrdinalIgnoreCase))
                {
                    if (Callback_Modify_textBox_CIMI != null)
                    {
                        Callback_Modify_textBox_CIMI("");
                    }
                }
            }
            else if (OK_Num >= 0)//判断字符串中是否包含OK
            {
                if (sendStr.Equals("AT+CIMI\r", StringComparison.OrdinalIgnoreCase))
                {
                    int CMEE_Num = s.IndexOf("\r\n", StringComparison.CurrentCultureIgnoreCase);
                    int startNum = CMEE_Num + "\r\n".Length;
                    int strlength = s.Substring(startNum).IndexOf("\r\n");
                    if (OK_Num > 0)
                    {
                        if (Callback_Modify_textBox_CIMI != null)
                        {
                            Callback_Modify_textBox_CIMI("<IMSI>=" + s.Substring(startNum, strlength));
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_CIMI != null)
                        {
                            Callback_Modify_textBox_CIMI("");
                        }
                    }
                }
                    
            }
            else
            {
                logStr += "返回的數據中既不包含OK，又不包含ERROR，無法解碼！";
            }
            Logger.WriteLog(logStr);
        }

        public delegate_EventHandler_textBox_CIMI Callback_Modify_textBox_CIMI = null;

        //--------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// AT+QCCID应答解码
        /// </summary>
        private void decode_QCCID(byte[] recvData)//AT+QCCID应答解码
        {
            string s = System.Text.Encoding.ASCII.GetString(recvData);
            string logStr = "[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" +
                            "[Rx(char)]=" + s.Replace("\r\n", "<CR><LF>");
            int Error_Num = s.IndexOf("ERROR", StringComparison.CurrentCultureIgnoreCase);
            int OK_Num = s.IndexOf("OK", StringComparison.CurrentCultureIgnoreCase);
            string sendStr = byteToHexStr(bC25Data_Sub.sendDataBuf);
            if (Error_Num >= 0)//判断字符串中是否包含Error
            {
                if (Callback_Modify_textBox_QCCID != null)
                {
                    Callback_Modify_textBox_QCCID("");
                }
            }
            else if (OK_Num >= 0)//判断字符串中是否包含OK
            {
                int CMEE_Num = s.IndexOf("+QCCID: ", StringComparison.CurrentCultureIgnoreCase);
                if (OK_Num > 0 && CMEE_Num > 0)
                {
                    int startNum = CMEE_Num + "+QCCID: ".Length;
                    int strlength = s.Substring(startNum).IndexOf("\r\n");
                    if (startNum < OK_Num)
                    {
                        if (Callback_Modify_textBox_QCCID != null)
                        {
                            Callback_Modify_textBox_QCCID("<ICCID>: USIM卡識別號=" + s.Substring(startNum, strlength));
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_QCCID != null)
                        {
                            Callback_Modify_textBox_QCCID("");
                        }
                    }
                }
                else
                {
                    if (Callback_Modify_textBox_QCCID != null)
                    {
                        Callback_Modify_textBox_QCCID("");
                    }
                }
            }
            else
            {
                logStr += "返回的數據中既不包含OK，又不包含ERROR，無法解碼！";
            }
            Logger.WriteLog(logStr);
        }

        public delegate_EventHandler_textBox_QCCID Callback_Modify_textBox_QCCID = null;


        //--------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// AT+CSCON应答解码
        /// </summary>
        private void decode_CSCON(byte[] recvData)//AT+QCCID应答解码
        {
            string s = System.Text.Encoding.ASCII.GetString(recvData);
            string logStr = "[Rx(byte)]=" + byteToHexStr(recvData).Replace(",", " ") + "\r\n" +
                            "[Rx(char)]=" + s.Replace("\r\n", "<CR><LF>");
            int Error_Num = s.IndexOf("ERROR", StringComparison.CurrentCultureIgnoreCase);
            int OK_Num = s.IndexOf("OK", StringComparison.CurrentCultureIgnoreCase);
            string sendStr = byteToHexStr(bC25Data_Sub.sendDataBuf);
            if (Error_Num >= 0)//判断字符串中是否包含Error
            {
                if (Callback_Modify_textBox_CSCON1 != null)
                {
                    Callback_Modify_textBox_CSCON1("");
                }
            }
            else if (OK_Num >= 0)//判断字符串中是否包含OK
            {
                int CMEE_Num = s.IndexOf("+CSCON: ", StringComparison.CurrentCultureIgnoreCase);
                if (OK_Num > 0 && CMEE_Num > 0)
                {
                    int startNum = CMEE_Num + "+QCCID: ".Length;
                    int strlength = s.Substring(startNum).IndexOf("\r\n");
                    if (startNum < OK_Num)
                    {
                        if (Callback_Modify_textBox_CSCON1 != null)
                        {
                            Callback_Modify_textBox_CSCON1(s.Substring(startNum, strlength));
                        }
                    }
                    else
                    {
                        if (Callback_Modify_textBox_CSCON1 != null)
                        {
                            Callback_Modify_textBox_CSCON1("");
                        }
                    }
                }
                else
                {
                    if (Callback_Modify_textBox_CSCON1 != null)
                    {
                        Callback_Modify_textBox_CSCON1("");
                    }
                }
            }
            else
            {
                logStr += "返回的數據中既不包含OK，又不包含ERROR，無法解碼！";
            }
            Logger.WriteLog(logStr);
        }

        public delegate_EventHandler_textBox_CSCON1 Callback_Modify_textBox_CSCON1 = null;




    }
}